package com.weidey.blogWeb.controller;

import com.weidey.blog.domain.Comment;
import com.weidey.blogWeb.domain.param.CommentParam;
import com.weidey.blogWeb.service.IWebCommentService;
import com.weidey.common.core.domain.AjaxResult;
import com.weidey.common.utils.SecurityUtils;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;

//@Api(tags = "评论")
@RestController
@RequestMapping("/blog/web/comment")
public class WebCommentController {

    @Resource
    private IWebCommentService commentService;
    //TODO:博客评论
    /**
     * 评论
     */
    @PostMapping("/begin")
//    @ApiOperation("评论")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult commentBegin(@RequestBody @Validated CommentParam commentParam){
        //进行初始化
        Comment comment = new Comment();
        Long userId = SecurityUtils.getUserId();
        //进行参数设置
        comment.setUid(userId);
        comment.setCreateTime(new Date());
        comment.setComment(commentParam.getComment());
        comment.setArticleId(commentParam.getArticleId());
        comment.setPid(commentParam.getPid());
        comment.setTarget(commentParam.getTarget());
        comment.setUsername(commentParam.getUsername());
        comment.setState("Y");
        //存储评论
        try {
            commentService.save(comment);
        }catch (Exception e){
            AjaxResult.error("操作异常,请联系管理员");
        }
        return AjaxResult.success("发表成功");
    }

    /**
     * 获取评论
     */
    @GetMapping("/select")
//    @ApiOperation("获取评论")
    public AjaxResult commentGet(@RequestParam("commentId") Integer commentId ,@RequestParam("order") Boolean order){
        return commentService.commentGet(commentId,order);
    }

}
